package com.itheima.leyou.dao;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Repository
public class OrderDao {

    @Resource
    JdbcTemplate jdbcTemplate;

    public Map<String, Object> getOrder(String orderid){

        String sql = "SELECT order_id, total_fee, actual_fee, promotion_ids, payment_type, post_fee, user_id, invoice_type," +
                "source_type, status, create_time, pay_time, consign_time, end_time, close_time, comment_time, update_time " +
                "FROM tb_order " +
                "WHERE order_id = "+orderid;
        ArrayList<Map<String, Object>> masterList = new ArrayList<>();
        masterList = (ArrayList<Map<String,Object>>) jdbcTemplate.queryForList(sql);

        sql = "SELECT tb_order_detail.id, tb_order_detail.order_id, tb_order_detail.sku_id, tb_order_detail.num, " +
                "tb_order_detail.title, tb_order_detail.own_spec, tb_order_detail.price, tb_order_detail.image, " +
                "tb_order_detail.create_time, tb_order_detail.update_time, tb_sku.title, tb_sku.own_spec " +
                "FROM tb_order_detail INNER JOIN tb_sku ON tb_order_detail.sku_id = tb_sku.id " +
                "WHERE tb_order_detail.order_id = "+orderid;
        ArrayList<Map<String, Object>> detailList = new ArrayList<>();
        detailList = (ArrayList<Map<String,Object>>) jdbcTemplate.queryForList(sql);

        Map<String, Object> detailMap = new HashMap<>();
        detailMap.put("detailList", detailList);

        sql = "SELECT order_id, logistics_number, logistics_company, addressee, phone, province, city, district, " +
                "street, postcode, create_time, update_time " +
                "FROM tb_order_logistics " +
                "WHERE order_id = "+orderid;
        ArrayList<Map<String, Object>> logisticsList = new ArrayList<>();
        logisticsList = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);

        Map<String, Object> logisticsMap = new HashMap<>();
        logisticsMap.put("logisticsList", logisticsList);

        Map<String, Object> order = new HashMap<>();
        order.put("master", masterList);
        order.put("detail", detailMap);
        order.put("logistics", logisticsMap);

        return order;
    }

    public Map<String, Object> insertOrder(Map<String, Object> orderInfo){

        Map<String, Object> resultMap = new HashMap<>();
        String order_id = (String) orderInfo.get("order_id");          //订单id
        Object total_fee = orderInfo.get("total_fee");                 //总金额
        Object actual_fee =  orderInfo.get("actual_fee");              //实付金额
        String promotion_ids = (String) orderInfo.get("promotion_ids");//优惠活动id
        Object payment_type =  orderInfo.get("payment_type");          //支付类型
        Object post_fee =  orderInfo.get("post_fee");                  //邮费
        Object user_id =  orderInfo.get("user_id");                    //用户id
        Object invoice_type =  orderInfo.get("invoice_type");          //发票类型
        Object source_type =  orderInfo.get("source_type");            //来源类型
        Object status =  orderInfo.get("status");                      //订单状态

        String sql = "INSERT INTO tb_order (order_id, total_fee, actual_fee, " +
                "promotion_ids, payment_type, post_fee, user_id, invoice_type, source_type," +
                "status, create_time, pay_time) " +
                "VALUES ('"+order_id+"', "+total_fee+", "+actual_fee+", " +
                "'"+promotion_ids+"', "+payment_type+", "+post_fee+", "+user_id+", "+invoice_type+", "+source_type+"," +
                ""+status+", NOW(), NULL)";

        boolean result = jdbcTemplate.update(sql)==1;
        if (!result){
            resultMap.put("result", false);
            resultMap.put("msg", "写入订单主表失败！");
            return resultMap;
        }

        Object sku_id = orderInfo.get("sku_id");
        int num = 1;//(int) detailMap.get("num")
        String title = (String) orderInfo.get("title");
        String own_spec = (String) orderInfo.get("own_spec");
        String image = (String) orderInfo.get("image");

        sql = "INSERT INTO tb_order_detail (order_id, sku_id, num, title, own_spec," +
                "price, image, create_time) " +
                "VALUES ('"+order_id+"', '"+sku_id+"', "+num+", '"+title+"', '"+own_spec+"'," +
                ""+actual_fee+", '"+image+"', NOW())";
        result = jdbcTemplate.update(sql)==1;

        if (!result){
            resultMap.put("result", false);
            resultMap.put("msg", "写入订单明细表失败！");
            return resultMap;
        }

        String logistics_number = null;
        String logistics_company = null;
        String addressee = null;
        String phone = null;
        String province = null;
        String city = null;
        String district = null;
        String street = null;
        int postcode = 0;

        sql = "INSERT INTO tb_order_logistics (order_id, logistics_number, " +
                "logistics_company, addressee, phone, province, " +
                "city, district, street, postcode, create_time) " +
                "VALUES ('"+order_id+"', '"+logistics_number+"', " +
                "'"+logistics_company+"', '"+addressee+"', '"+phone+"', '"+province+"', " +
                "'"+city+"', '"+district+"', '"+street+"', '"+postcode+"', now())";
        result = jdbcTemplate.update(sql)==1;

        if (!result){
            resultMap.put("result", false);
            resultMap.put("msg", "写入订单收货表失败！");
            return resultMap;
        }

        resultMap.put("result", true);
        resultMap.put("msg", "");
        resultMap.put("order_id", order_id);
        return resultMap;
    }

    public boolean updateOrderStatus(String orderid){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String pay_time = simpleDateFormat.format(new Date());
        String sql = String.format("update tb_order set status = 2, pay_time = '%s' where order_id = %s", pay_time, orderid);
        return jdbcTemplate.update(sql)==1;
    }

    public String getsku_id(String orderid){
        String sql = "select sku_id from tb_order_detail where order_id = "+orderid;
        ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
        return list.get(0).get("sku_id").toString();
    }

    /**
     * 根据sku_id查询订单明细
     * @param sku_id
     * @return
     */
    public List<Map<String,Object>> getOrderDetailListBySkuId(String sku_id){
        String sql = "select order_id from tb_order_detail where sku_id = "+sku_id + " ORDER BY order_id DESC ";
        ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
        return list;
    }
}
